package hy.planegame.sprite

import android.graphics.Canvas
import hy.planegame.utils.enemyBullet
import hy.planegame.utils.paint
import hy.planegame.utils.screenHeight

/**
 * Created time : 2017/9/1 11:34.
 * @author HY
 */
class EnemyBullet(private val centerX: Int, private val top: Int) : Sprite<EnemyBullet>(), Comparable<EnemyBullet> {
    override fun compareTo(other: EnemyBullet): Int = centerX * top - other.centerX * other.top

    private val width = enemyBullet.width
    private val height =enemyBullet.height

    private val speed = 30
    private var isDraw = true
    private var y = top


    override fun draw(canvas: Canvas?) {
        canvas ?: return
        if (isDraw) {
            canvas.drawBitmap(enemyBullet, centerX - width / 2f, y.toFloat(), paint)
        }
    }

    override fun logic() {
        if (isDraw) {
            if (y >= screenHeight) {
                destroy()
            } else {
                y += speed
            }
        }
    }

    private var _onDestroy: (() -> Unit)? = null
    fun onDestroy(callback: () -> Unit) {
        _onDestroy = callback
    }

    override fun destroy() {
        isDraw = false
//        if (null != bitmap) {
//            bitmap.recycle()
//            bitmap = null
//        }
        _onDestroy?.invoke()
    }
}